package fr.utbm.gi.vi51.project.gui;

import java.awt.Component;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.WindowEvent;
import java.awt.event.WindowListener;
import java.util.ArrayList;

import javax.swing.Box;
import javax.swing.BoxLayout;
import javax.swing.ButtonGroup;
import javax.swing.JButton;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JRadioButton;

import org.janusproject.kernel.agent.Kernels;

import fr.utbm.gi.vi51.project.agent.Marine;
import fr.utbm.gi.vi51.project.environment.WorldModel;
import fr.utbm.gi.vi51.project.formations.DeltaFormation;
import fr.utbm.gi.vi51.project.formations.FormationBody;
import fr.utbm.gi.vi51.project.formations.FormationOrientation;
import fr.utbm.gi.vi51.project.formations.LineFormation;
import fr.utbm.gi.vi51.project.formations.SplitFormation;
import fr.utbm.gi.vi51.project.formations.SpreadFormation;
import fr.utbm.gi.vi51.project.formations.SquareFormation;

public class SplitFormationDialog extends JDialog implements ActionListener, WindowListener {

	private static final long serialVersionUID = -4882168050759689404L;

	private static int selectedRadioButton = 2;

	private JPanel subformationCount2Panel;
	private JComboBox<String> box21, box22;

	private JPanel subformationCount3Panel;
	private JComboBox<String> box31, box32, box33;

	private JLabel subformationCount4Panel;
//	private JComboBox<String> box41, box42, box43, box44;

	public SplitFormationDialog() {
		Kernels.get().pause();
		// Set bounds and Title
		setTitle("Select Subformations"); //$NON-NLS-1$
		this.setBounds(400, 200, 300, 300);
		this.addWindowListener(this);

		// We create a bottom JPanel to place everything on.
		JPanel totalGUI = new JPanel();
		totalGUI.setLayout(new BoxLayout(totalGUI, BoxLayout.PAGE_AXIS));

		// Add Intro
		JLabel intro = new JLabel("Please select your subformations :"); //$NON-NLS-1$
		intro.setAlignmentX(Component.CENTER_ALIGNMENT);
		totalGUI.add(intro);

		// Add RadioButton Panel
		JPanel numberPanel = new JPanel();
		numberPanel.setLayout(new BoxLayout(numberPanel, BoxLayout.LINE_AXIS));

		JLabel numberText = new JLabel("Number of Subformations : "); //$NON-NLS-1$
		numberPanel.add(numberText);

		JRadioButton radioButton1 = new JRadioButton("2"); //$NON-NLS-1$
		radioButton1.setActionCommand("2"); //$NON-NLS-1$
		radioButton1.addActionListener(SplitFormationDialog.this);
		radioButton1.setSelected(true);

		JRadioButton radioButton2 = new JRadioButton("3"); //$NON-NLS-1$
		radioButton2.addActionListener(SplitFormationDialog.this);
		radioButton2.setActionCommand("3"); //$NON-NLS-1$

		JRadioButton radioButton3 = new JRadioButton("4"); //$NON-NLS-1$
		radioButton3.addActionListener(SplitFormationDialog.this);
		radioButton3.setActionCommand("4"); //$NON-NLS-1$

		final ButtonGroup group = new ButtonGroup();
		group.add(radioButton1);
		group.add(radioButton2);
		group.add(radioButton3);

		numberPanel.add(Box.createRigidArea(new Dimension(10, 0)));
		numberPanel.add(radioButton1);
		numberPanel.add(radioButton2);
		numberPanel.add(radioButton3);

		totalGUI.add(Box.createRigidArea(new Dimension(0, 5)));
		totalGUI.add(numberPanel);

		/*
		 * Formation Type pannels
		 */
		String[] formationTypes = { "Line", "Square", "Delta", "Spread" }; //$NON-NLS-1$ //$NON-NLS-2$ //$NON-NLS-3$ //$NON-NLS-4$

		// 2 subformations
		this.subformationCount2Panel = new JPanel();
		this.subformationCount2Panel.setLayout(new BoxLayout(this.subformationCount2Panel, BoxLayout.PAGE_AXIS));
		this.subformationCount2Panel.setAlignmentX(Component.CENTER_ALIGNMENT);

		JLabel text21 = new JLabel("Northern Formation"); //$NON-NLS-1$
		this.subformationCount2Panel.add(text21);

		this.box21 = new JComboBox<>(formationTypes);
		this.box21.setMaximumSize(new Dimension(110, 30));
		this.box21.setAlignmentX(Component.LEFT_ALIGNMENT);
		this.subformationCount2Panel.add(this.box21);

		this.subformationCount2Panel.add(Box.createRigidArea(new Dimension(0, 10)));

		JLabel text22 = new JLabel("Southern Formation"); //$NON-NLS-1$
		this.subformationCount2Panel.add(text22);

		this.box22 = new JComboBox<>(formationTypes);
		this.box22.setMaximumSize(new Dimension(110, 30));
		this.box22.setAlignmentX(Component.LEFT_ALIGNMENT);
		this.subformationCount2Panel.add(this.box22);

		totalGUI.add(this.subformationCount2Panel);

		// 3 subformations
		this.subformationCount3Panel = new JPanel();
		this.subformationCount3Panel.setLayout(new BoxLayout(this.subformationCount3Panel, BoxLayout.PAGE_AXIS));
		this.subformationCount3Panel.setAlignmentX(Component.CENTER_ALIGNMENT);

		JLabel text31 = new JLabel("Northeastern Formation"); //$NON-NLS-1$
		this.subformationCount3Panel.add(text31);

		this.box31 = new JComboBox<>(formationTypes);
		this.box31.setMaximumSize(new Dimension(130, 30));
		this.box31.setAlignmentX(Component.LEFT_ALIGNMENT);
		this.subformationCount3Panel.add(this.box31);

		this.subformationCount3Panel.add(Box.createRigidArea(new Dimension(0, 10)));

		JLabel text32 = new JLabel("Southeastern Formation"); //$NON-NLS-1$
		this.subformationCount3Panel.add(text32);

		this.box32 = new JComboBox<>(formationTypes);
		this.box32.setMaximumSize(new Dimension(130, 30));
		this.box32.setAlignmentX(Component.LEFT_ALIGNMENT);
		this.subformationCount3Panel.add(this.box32);

		this.subformationCount3Panel.add(Box.createRigidArea(new Dimension(0, 10)));

		JLabel text33 = new JLabel("Western Formation"); //$NON-NLS-1$
		this.subformationCount3Panel.add(text33);

		this.box33 = new JComboBox<>(formationTypes);
		this.box33.setMaximumSize(new Dimension(130, 30));
		this.box33.setAlignmentX(Component.LEFT_ALIGNMENT);
		this.subformationCount3Panel.add(this.box33);

		this.subformationCount3Panel.setVisible(false);
		totalGUI.add(this.subformationCount3Panel);

		// 4 subformations
		this.subformationCount4Panel = new JLabel("Not implemented yet"); //$NON-NLS-1$
		this.subformationCount4Panel.setAlignmentX(Component.CENTER_ALIGNMENT);
		this.subformationCount4Panel.setVisible(false);
		totalGUI.add(this.subformationCount4Panel);

		// Add Buttons
		JPanel buttonPanel = new JPanel();
		buttonPanel.setLayout(new BoxLayout(buttonPanel, BoxLayout.LINE_AXIS));
		JButton ok = new JButton("OK"); //$NON-NLS-1$
		ok.addActionListener(SplitFormationDialog.this);
		ok.setActionCommand("ok"); //$NON-NLS-1$
		buttonPanel.add(ok);

		buttonPanel.add(Box.createRigidArea(new Dimension(10, 0)));

		JButton cancel = new JButton("Cancel"); //$NON-NLS-1$
		cancel.addActionListener(SplitFormationDialog.this);
		cancel.setActionCommand("ko"); //$NON-NLS-1$
		buttonPanel.add(cancel);

		totalGUI.add(Box.createRigidArea(new Dimension(0, 10)));
		totalGUI.add(buttonPanel);

		// Add the GUI to the Dialog
		this.add(totalGUI);
	}

	@Override
	public void actionPerformed(ActionEvent event) {
		if (event.getActionCommand().equals("2")) { //$NON-NLS-1$
			SplitFormationDialog.selectedRadioButton = 2;
			this.subformationCount2Panel.setVisible(true);
			this.subformationCount3Panel.setVisible(false);
			this.subformationCount4Panel.setVisible(false);
		} else if (event.getActionCommand().equals("3")) { //$NON-NLS-1$
			SplitFormationDialog.selectedRadioButton = 3;
			this.subformationCount2Panel.setVisible(false);
			this.subformationCount3Panel.setVisible(true);
			this.subformationCount4Panel.setVisible(false);
		} else if (event.getActionCommand().equals("4")) { //$NON-NLS-1$
			SplitFormationDialog.selectedRadioButton = 4;
			this.subformationCount2Panel.setVisible(false);
			this.subformationCount3Panel.setVisible(false);
			this.subformationCount4Panel.setVisible(true);
		} else if (event.getActionCommand().equals("ok")) { //$NON-NLS-1$
			switch (selectedRadioButton) {
			case 2:
				ArrayList<FormationBody> subFormations2 = new ArrayList<>();
				FormationBody northFormation2 = null;
				FormationBody southFormation2 = null;
				
				switch (this.box21.getSelectedIndex()) {
				case 0:
					northFormation2 = new LineFormation(FormationOrientation.SOUTH);
					break;
				case 1:
					northFormation2 = new SquareFormation(FormationOrientation.SOUTH);
					break;
				case 2:
					northFormation2 = new DeltaFormation(FormationOrientation.SOUTH);
					break;
				case 3:
					northFormation2 = new SpreadFormation(FormationOrientation.SOUTH);
					break;
				default:
					break;
				}
				
				switch (this.box22.getSelectedIndex()) {
				case 0:
					southFormation2 = new LineFormation(FormationOrientation.NORTH);
					break;
				case 1:
					southFormation2 = new SquareFormation(FormationOrientation.NORTH);
					break;
				case 2:
					southFormation2 = new DeltaFormation(FormationOrientation.NORTH);
					break;
				case 3:
					southFormation2 = new SpreadFormation(FormationOrientation.NORTH);
					break;
				default:
					break;
				}

				subFormations2.add(northFormation2);
				subFormations2.add(southFormation2);
				FormationBody splitBody2 = new SplitFormation(FormationOrientation.EAST, subFormations2);
				splitBody2.scale(WorldModel.getMarines().size());
				Marine.formationBody = splitBody2;
				
				break;
			case 3:
				ArrayList<FormationBody> subFormations3 = new ArrayList<>();
				FormationBody northFormation3 = null;
				FormationBody southFormation3 = null;
				FormationBody westFormation3 = null;
				
				switch (this.box31.getSelectedIndex()) {
				case 0:
					northFormation3 = new LineFormation(FormationOrientation.SOUTH);
					break;
				case 1:
					northFormation3 = new SquareFormation(FormationOrientation.SOUTH);
					break;
				case 2:
					northFormation3 = new DeltaFormation(FormationOrientation.SOUTH);
					break;
				case 3:
					northFormation3 = new SpreadFormation(FormationOrientation.SOUTH);
					break;
				default:
					break;
				}
				
				switch (this.box32.getSelectedIndex()) {
				case 0:
					southFormation3 = new LineFormation(FormationOrientation.NORTH);
					break;
				case 1:
					southFormation3 = new SquareFormation(FormationOrientation.NORTH);
					break;
				case 2:
					southFormation3 = new DeltaFormation(FormationOrientation.NORTH);
					break;
				case 3:
					southFormation3 = new SpreadFormation(FormationOrientation.NORTH);
					break;
				default:
					break;
				}
				
				switch (this.box33.getSelectedIndex()) {
				case 0:
					westFormation3 = new LineFormation(FormationOrientation.EAST);
					break;
				case 1:
					westFormation3 = new SquareFormation(FormationOrientation.EAST);
					break;
				case 2:
					westFormation3 = new DeltaFormation(FormationOrientation.EAST);
					break;
				case 3:
					westFormation3 = new SpreadFormation(FormationOrientation.EAST);
					break;
				default:
					break;
				}
				
				subFormations3.add(northFormation3);
				subFormations3.add(southFormation3);
				subFormations3.add(westFormation3);
				FormationBody splitBody3 = new SplitFormation(FormationOrientation.EAST, subFormations3);
				splitBody3.scale(WorldModel.getMarines().size());
				Marine.formationBody = splitBody3;
				
				break;
			case 4:
				// TODO
				break;
			default:
				break;
			}
			SplitFormationDialog.this.setVisible(false);
			Kernels.get().resume();
		} else if (event.getActionCommand().equals("ko")) { //$NON-NLS-1$
			SplitFormationDialog.this.setVisible(false);
			Kernels.get().resume();
		}
	}

	@Override
	public void windowOpened(WindowEvent e) {
		// Do nothing
	}

	@Override
	public void windowClosing(WindowEvent e) {
		Kernels.get().resume();
	}

	@Override
	public void windowClosed(WindowEvent e) {
		// Do nothing
	}

	@Override
	public void windowIconified(WindowEvent e) {
		// Do nothing
	}

	@Override
	public void windowDeiconified(WindowEvent e) {
		// Do nothing
	}

	@Override
	public void windowActivated(WindowEvent e) {
		// Do nothing
	}

	@Override
	public void windowDeactivated(WindowEvent e) {
		// Do nothing
	}

}
